/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketmaster.dataaccess;

import com.sun.rowset.CachedRowSetImpl;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;

/**
 *
 * @author Wei Liu
 */
public class DataAccess
{
    public static RowSet query(String sql) throws IOException, ClassNotFoundException, SQLException
    {
        //DataSource.init();
        Connection conn = DataSource.getConnection();
        ResultSet rs;
        CachedRowSet crs;

        try
        {
            Statement stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            crs = new CachedRowSetImpl();
            crs.populate(rs);
        }
        catch(Exception ex)
        {
            return null;
        }
        finally
        {
            conn.close();
        }

        return crs;
    }

    public static int execute(String sql) throws IOException, ClassNotFoundException, SQLException
    {
        //DataSource.init();
        Connection conn = DataSource.getConnection();
        int result;

        try
        {
            Statement stat = conn.createStatement();
            result = stat.executeUpdate(sql);
        }
        catch(Exception ex)
        {
            return 0;
        }
        finally
        {
            conn.close();
        }

        return result;
    }
}
